package com.geek.factory.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.dto.DeviceRepairReportDTO;
import com.geek.factory.entity.DeviceRepairReport;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface DeviceRepairReportMapper  extends BaseMapper<DeviceRepairReport> {

    @Select("SELECT r.id, r.device_name, r.device_type, r.product_line_id, r.description, " +
            "r.status, r.submitter, r.submit_time, r.process_instance_id, l.line_name AS line_name " +
            "FROM device_repair_report r " +
            "LEFT JOIN product_line l ON r.product_line_id = l.id")
    List<DeviceRepairReportDTO> selectAllReportsWithLineName();

    @Select("SELECT r.id, r.device_name, r.device_type, r.product_line_id, r.description, " +
            "r.status, r.submitter, r.submit_time, r.process_instance_id, l.line_name AS line_name " +
            "FROM device_repair_report r " +
            "LEFT JOIN product_line l ON r.product_line_id = l.id " +
            "WHERE r.id = #{id}")
    DeviceRepairReportDTO selectReportWithLineName(@Param("id") Long id);

    @Select("SELECT r.id, r.device_name, r.device_type, r.product_line_id, r.description, " +
            "r.status, r.submitter, r.submit_time, r.process_instance_id, l.line_name AS line_name " +
            "FROM device_repair_report r " +
            "LEFT JOIN product_line l ON r.product_line_id = l.id")
    IPage<DeviceRepairReportDTO> selectPagedReportsWithLineName(Page<DeviceRepairReportDTO> page);

    @Update("UPDATE device_repair_report SET status = #{status} WHERE id = #{reportId}")
    int updateStatus(@Param("reportId") Long reportId, @Param("status") String status);

}
